package com.orangelife.mobile.goodsrent.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.DecimalUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsrent.activity.RentDetailActivity;
import com.orangelife.mobile.goodsrent.biz.GoodsInfoCallBack;
import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;
import com.orangelife.mobile.util.ToastHelper;
import com.thirdsdk.qiniu.QiniuHelper;

import java.util.List;

/**
 * Created by Administrator on 2015/12/14.
 */
public class RentAdapter extends FatherAdapter<List<GoodsInfo>> {
    private Context context;
    private List<List<GoodsInfo>> mlist;
    private GoodsInfoCallBack goodsInfoCallBack;
    private ImageDownloader image = ImageDownloader.getImageDownloader();

    @Override
    public BaseAdapter setList(List<List<GoodsInfo>> list) {
        this.mlist = list;
        return RentAdapter.this;
    }

    public RentAdapter(Context context, List<List<GoodsInfo>> list, GoodsInfoCallBack goodsInfoCallBack) {
        this.goodsInfoCallBack = goodsInfoCallBack;
        this.context = context;
        this.mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_goodsrent_double, null);
            holder.ivPhoto1 = (ImageView) convertView.findViewById(R.id.ivPhoto1);
            holder.tvGoodsName1 = (TextView) convertView.findViewById(R.id.tvGoodsName1);
            holder.tvStandard1 = (TextView) convertView.findViewById(R.id.tvStandard1);
            holder.tvPriceDiscount1 = (TextView) convertView.findViewById(R.id.tvPrice1);
            holder.tvPrice1 = (TextView) convertView.findViewById(R.id.tvPrice2);
            holder.llHotGoods1 = (LinearLayout) convertView.findViewById(R.id.llHotGoods1);
            holder.ibGoodsAdd1 = (ImageButton) convertView.findViewById(R.id.ibGoodsAdd);

            holder.ivPhoto2 = (ImageView) convertView.findViewById(R.id.ivPhoto2);
            holder.tvGoodsName2 = (TextView) convertView.findViewById(R.id.tvGoodsName2);
            holder.tvStandard2 = (TextView) convertView.findViewById(R.id.tvStandard2);
            holder.tvPriceDiscount2 = (TextView) convertView.findViewById(R.id.tvPrice1_2);
            holder.tvPrice2 = (TextView) convertView.findViewById(R.id.tvPrice2_2);
            holder.llHotGoods2 = (LinearLayout) convertView.findViewById(R.id.llHotGoods2);
            holder.ibGoodsAdd2 = (ImageButton) convertView.findViewById(R.id.ibGoodsAdd2);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /* listview填数据 */
        List<GoodsInfo> listmap = mlist.get(position);
        if (listmap.size() > 1) {
            GoodsInfo goodsInfo = listmap.get(0);
            holder.tvGoodsName1.setText(goodsInfo.getGoodName());
            holder.tvStandard1.setText(Constant.RMB_FLAG + goodsInfo.getPrice() + "/" + goodsInfo.getUnit());
            holder.tvPriceDiscount1.setText(Constant.RMB_FLAG + goodsInfo.getPrice());
            holder.tvPrice1.setText(Constant.RMB_FLAG + goodsInfo.getPrice());
            holder.tvPrice1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(goodsInfo.getGoodsLogo(), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), holder.ivPhoto1);
            holder.ibGoodsAdd1.setOnClickListener(new listener(goodsInfo));
            holder.llHotGoods1.setOnClickListener(new listener(goodsInfo));

            GoodsInfo goodsInfo1 = listmap.get(1);

            holder.tvGoodsName2.setText(listmap.get(1).getGoodName());
            holder.tvStandard2.setText(Constant.RMB_FLAG + listmap.get(1).getPrice() + "/" + goodsInfo1.getUnit());
            holder.tvPriceDiscount2.setText(Constant.RMB_FLAG + goodsInfo1.getPrice());
            holder.tvPrice2.setText(Constant.RMB_FLAG + goodsInfo1.getPrice());
            holder.tvPrice2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(goodsInfo1.getGoodsLogo(), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), holder.ivPhoto2);
            holder.llHotGoods2.setOnClickListener(new listener(goodsInfo1));
            holder.ibGoodsAdd2.setOnClickListener(new listener(goodsInfo1));
            holder.llHotGoods2.setVisibility(View.VISIBLE);
        } else if (listmap.size() == 1) {
            GoodsInfo goodsInfo = listmap.get(0);

            holder.tvGoodsName1.setText(goodsInfo.getGoodName());
            holder.tvStandard1.setText(Constant.RMB_FLAG + goodsInfo.getPrice() + "/" + goodsInfo.getUnit());
            holder.tvPriceDiscount1.setText(Constant.RMB_FLAG + goodsInfo.getPrice());
            holder.tvPrice1.setText(Constant.RMB_FLAG + goodsInfo.getPrice());
            holder.tvPrice1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(goodsInfo.getGoodsLogo(), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), holder.ivPhoto1);
            holder.llHotGoods1.setOnClickListener(new listener(goodsInfo));
            holder.ibGoodsAdd1.setOnClickListener(new listener(goodsInfo));
            holder.llHotGoods2.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    class listener implements View.OnClickListener {

        private GoodsInfo goodsInfo;

        public listener(GoodsInfo goodsInfo) {
            this.goodsInfo = goodsInfo;
        }

        @Override
        public void onClick(View view) {

            if (!NetworkUtil.isConnectedNet(context)) {
                Toast.makeText(
                        context,
                        context.getResources().getString(
                                R.string.net_disconnected),
                        Toast.LENGTH_SHORT).show();
                return;
            }
            switch (view.getId()) {
                case R.id.llHotGoods1:
                case R.id.llHotGoods2:
                    Intent intent = new Intent();
                    intent.putExtra("detail", goodsInfo);
                    intent.setClass(context, RentDetailActivity.class);
                    context.startActivity(intent);
                    break;
                case R.id.ibGoodsAdd:
                case R.id.ibGoodsAdd2:
                    /**计算单个物品的租借总价*/
                    addGoodsInfo(goodsInfo);
                    if (GoodsDao.getInstance().isExistGood(goodsInfo.getrGoodID())) {
                        ToastHelper.getInstance()._toast("购物车已存在此商品");
                    } else {
                        Long uid = GoodsDao.getInstance().insertUserInfo(goodsInfo);
                        if (uid != -1) {/**插入成功*/
                            goodsInfoCallBack.startShopCardAnim(view);
                            /**更新物品总价*/
                            goodsInfoCallBack.getGoodsTotalPrice(GoodsDao.getInstance().getGoodsTotalPrice());
                        }
                    }
                    break;
            }
        }

    }

    private void addGoodsInfo(GoodsInfo goodsInfo) {
        goodsInfo.setUnitPrice(DecimalUtil.multiply(goodsInfo.getDay(), goodsInfo.getPrice()));
    }

    class ViewHolder {
        /**
         * 照片
         */
        private ImageView ivPhoto1;
        /**
         * 标题
         */
        private TextView tvGoodsName1;
        /**
         * 重量
         */
        private TextView tvStandard1;
        /**
         * 折扣价
         */
        private TextView tvPriceDiscount1;
        /**
         * 现价
         */
        private TextView tvPrice1;

        private ImageButton ibGoodsAdd1;

        /**
         * 点击事件
         */
        private LinearLayout llHotGoods1;


        /**
         * 照片
         */
        private ImageView ivPhoto2;
        /**
         * 标题
         */
        private TextView tvGoodsName2;
        /**
         * 重量
         */
        private TextView tvStandard2;
        /**
         * 折扣价
         */
        private TextView tvPriceDiscount2;
        /**
         * 现价
         */
        private TextView tvPrice2;

        private ImageButton ibGoodsAdd2;

        /**
         * 点击事件
         */
        private LinearLayout llHotGoods2;


    }
}
